package com.example.shipload_e.activitys.discharge_feedback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shipload_e.R;
import com.example.shipload_e.ShipMentWelcomeActivity;
import com.example.shipload_e.activitys.shipment_feedback.ShipmentFinishFormActivity;
import com.example.shipload_e.activitys.shipment_feedback.ShipmentFinishListActivity.MyAdapter;
import com.example.shipload_e.activitys.shipment_feedback.ShipmentFinishListActivity.ViewHolder;
import com.example.shipload_e.database_dao.DBUtil;
import com.example.shipload_e.util.SharePreferenceUtil;

public class DischargeFinishListActivity extends Activity {

	private ListView	                item_list;
	private List<Map<String, Object>>	mData;
	private ImageButton	                mGoBack;
	private TextView	                titleName;
	SharePreferenceUtil	                spu;
	private DischargeFinishListActivity	theActivity	= DischargeFinishListActivity.this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.common_massege_list_main);
		spu = new SharePreferenceUtil(theActivity);
		initView();
	}

	private void initView() {
		item_list = (ListView) findViewById(R.id.itemIist);
		mGoBack = (ImageButton) findViewById(R.id.m_goback);
		titleName = (TextView) findViewById(R.id.title_name);
		mGoBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(theActivity, ShipMentWelcomeActivity.class));
			}
		});
		titleName.setText(spu.getTitleName());
	}

	@Override
	protected void onStart() {
		super.onStart();
		initDatas();
	}

	private void initDatas() {
		try {
			if(getIntent().hasExtra("initResult")) {
				String result = getIntent().getStringExtra("initResult");
				spu.setResponseResult(result);
				JSONArray getResult;
				getResult = new JSONArray(result.toString());
				System.out.println(getResult);
				List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
				for(int i = 0;i < getResult.length();i++) {
					JSONObject o = new JSONObject(getResult.getString(i));
					String id = o.has("id") == false ? "" : o.getString("id");
					String subOrderCode = o.has("subOrderCode") == false ? "" : o
					        .getString("subOrderCode");
					String startPort = o.has("startPort") == false ? "" : o.getString("startPort");
					String endPort = o.has("endPort") == false ? "" : o.getString("endPort");
					String goods = o.has("goods") == false ? "" : o.getString("goods");
					String goodsVolume = o.has("goodsVolume") == false ? "" : o
					        .getString("goodsVolume");
					String goodsUnit = o.has("goodsUnit") == false ? "" : o.getString("goodsUnit");
					String shipName = o.has("shipName") == false ? "" : o.getString("shipName");
					String totalFreight = o.has("totalFreight") == false ? "" : o
					        .getString("totalFreight");
					String finishFreight = o.has("finishFreight") == false ? "" : o
					        .getString("finishFreight");
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("id", id);
					map.put("dingdanhao", subOrderCode);
					map.put("hangxian", startPort + "-" + endPort);
					map.put("chuanming", shipName);
					map.put("huowu", goods);
					map.put("shuliang", goodsVolume + goodsUnit);
					map.put("goodsUnit", goodsUnit);
					map.put("goodsVolume", goodsVolume);
					map.put("totalFreight", totalFreight);
					map.put("finishFreight", finishFreight);
					mapList.add(map);
				}

				item_list = (ListView) findViewById(R.id.itemIist);
				// 生成动态数组，并且转入数据
				mData = mapList;
				// 生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
				MyAdapter adapter = new MyAdapter(this);
				item_list.setAdapter(adapter);
			}

		} catch (JSONException e) {
			if(spu.getIsComeFromWelcome()) {
				Toast.makeText(theActivity, "暂无任务", Toast.LENGTH_SHORT).show();
			}
			startActivity(new Intent(theActivity, ShipMentWelcomeActivity.class));
			e.printStackTrace();
		}
		spu.setIsComeFromWelcome(false);
	}

	public final class ViewHolder {
		public TextView		dingDanHao, hangXian, chuanMing, huoWu, shuLiang,
		        alreadyShipmentCountT, notShipmentCountT;
		public LinearLayout	shipperItemLin;
	}

	public class MyAdapter extends BaseAdapter {

		private final LayoutInflater	mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@SuppressLint({"NewApi", "ResourceAsColor" })
		@Override
		public View getView(final int position,View convertView,ViewGroup parent) {

			ViewHolder holder = null;

			holder = new ViewHolder();

			convertView = mInflater.inflate(R.layout.shipper_saggese_list_item, null);
			holder.dingDanHao = (TextView) convertView.findViewById(R.id.ding_dan_hao);
			holder.hangXian = (TextView) convertView.findViewById(R.id.hang_xian);
			holder.chuanMing = (TextView) convertView.findViewById(R.id.chuan_ming);
			holder.huoWu = (TextView) convertView.findViewById(R.id.huo_wu);
			holder.shuLiang = (TextView) convertView.findViewById(R.id.shu_liang);
			holder.shipperItemLin = (LinearLayout) convertView.findViewById(R.id.shipper_item_lin);
			holder.alreadyShipmentCountT = (TextView) convertView
			        .findViewById(R.id.already_shipment_count);
			holder.notShipmentCountT = (TextView) convertView.findViewById(R.id.not_shipment_count);

			convertView.setTag(holder);
			if(position % 2 == 1) {
				holder.shipperItemLin.setBackgroundColor(getResources()
				        .getColor(R.color.list_item_color1));
			} else {
				holder.shipperItemLin.setBackgroundColor(getResources()
				        .getColor(R.color.list_item_color));
			}
			holder.dingDanHao.setText(mData.get(position).get("dingdanhao").toString());
			holder.hangXian.setText(mData.get(position).get("hangxian").toString());
			holder.chuanMing.setText(mData.get(position).get("chuanming").toString());
			holder.huoWu.setText(mData.get(position).get("huowu").toString());
			holder.shuLiang.setText(mData.get(position).get("shuliang").toString());
			holder.alreadyShipmentCountT.setText(mData.get(position).get("finishFreight")
			        .toString());
			holder.notShipmentCountT.setText(mData.get(position).get("totalFreight").toString());

			holder.shipperItemLin.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					System.out.println(position);
					Intent in = new Intent(theActivity, DischargeFinishFormActivity.class);
					in.putExtra("followId", mData.get(position).get("id").toString());
					in.putExtra("followResult", getIntent().getStringExtra("initResult"));
					in.putExtra("followUnit", mData.get(position).get("goodsUnit").toString());
					in.putExtra("followCount", mData.get(position).get("goodsVolume").toString());
					spu.setFollowId(mData.get(position).get("id").toString());
					spu.setFollowResult(getIntent().getStringExtra("initResult"));
					spu.setFollowCount(mData.get(position).get("goodsVolume").toString());
					spu.setFollowUnit(mData.get(position).get("goodsUnit").toString());
					// 清除图片数据库
					DBUtil.deleteAll(theActivity, "PHOTOS");
					startActivity(in);
				}
			});
			return convertView;
		}
	}

	public boolean onKeyDown(int keyCode,KeyEvent event) {

		if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			// 这里重写返回键
			startActivity(new Intent(theActivity, ShipMentWelcomeActivity.class));
			return true;
		}
		return false;

	}
}
